/*
 * @author:Brandon Wong
 */
package com.apps.services;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.LinkedList;
import java.util.List;

import com.apps.datastore.dao.CourseInformationObject;
import com.apps.datastore.dao.SectionInformationObject;
import com.apps.ubc.cc.model.BuildingModel;
import com.apps.ubc.cc.model.GoogleDirectionModel;
import com.apps.utils.WrapperUtils;


public class GoogleDirectionsService {

	private static final String GOOLGURL = "http://maps.googleapis.com/maps/api/directions/xml?origin=<source>&destination=<destination>&mode=walking&sensor=false";

	private String setURL(SectionInformationObject source,
			SectionInformationObject destination, String googleURL) {

		googleURL = replaceTag(source, "<source>", googleURL);
		googleURL = replaceTag(destination, "<destination>", googleURL);

		return googleURL;
	}

	private String replaceTag(SectionInformationObject section,
			String to_replace, String url) {

		// This is to get the lat/long
		UBCWayfindingService addrParser;
		BuildingModel addr;
		String buffer;

		addrParser = new UBCWayfindingService();
		addr = addrParser.search(section.getBuilding());

		buffer = addr.getLatitude();
		buffer = buffer.concat(",");
		buffer = buffer.concat(addr.getLongitude());

		return url.replace(to_replace, buffer);
	}

	public String setURL(String[] section, String to_replace, String url) {

		String buffer = "";

		if (section.length <= 2) {
			buffer = section[0];
			for (int i = 1; i < section.length; i++) {
				buffer = buffer.concat(",");
				buffer = buffer.concat(section[i]);
			}

		}

		return url.replace(to_replace, buffer);
	}

	private static GoogleDirectionModel getDirectionXml(String url)
			throws MalformedURLException, IOException {
		// just to make sure
		String q_address = url.replaceAll(" ", "\\+");
		System.out.println(q_address);

		BufferedReader br = new BufferedReader(new InputStreamReader(new URL(
				q_address).openStream()));
		String line = br.readLine();

		String buffer_time;
		//int time = 0;

		String buffer_distance;
		//double distance = 0;

		try {
			// Find the last </step>. That contains the
			// total distance and length
			StringBuilder buffer=null;
			while (line != null && !line.contains("<copyrights>")) {
					if(line.contains("</step>")){
						buffer=new StringBuilder();
						while(line!=null && !line.contains("</distance>")){
							buffer.append(line);
							line=br.readLine();
						}
					}else{
						line=br.readLine();
					}
			}
			//System.out.println(buffer.toString());
			//buffer should have the last <step>
			String duration=WrapperUtils.stringInBwtn("<duration>", "</duration>", buffer.toString());
			String time=WrapperUtils.stringInBwtn("<text>", "</text>", duration);
			
			String distance_text=WrapperUtils.stringInBwtn("<distance>", "/text>", buffer.toString());
			String distance= WrapperUtils.stringInBwtn("<text>", "<", distance_text);
			
			int duration_time=Integer.parseInt(time.split(" min")[0]);
			double duration_distance=Double.parseDouble(distance.split(" km")[0]);
			return new GoogleDirectionModel(duration_distance, duration_time);

		} finally {
			br.close();
		}

	}

	public GoogleDirectionModel getDirections(SectionInformationObject source,
			SectionInformationObject destination) {

		String url = GOOLGURL;

		url = setURL(source, destination, url);
		try {
			return getDirectionXml(url);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}
